// 封装HTTP 请求
async function httpRequest(url, method, headers = {}, body = null) {
  const options = {
      method: method,
      headers: {
          'Content-Type': 'application/json',
          ...headers
      }
  };

  if (body) {
      options.body = JSON.stringify(body);
  }

  try {
      const response = await fetch(url, options);
      if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
      }
      const data = await response.json();
      return data;
  } catch (error) {
      console.error('Error:', error);
      throw error;
  }
}

var infoList = {}

let currentUrl = new URL(window.location.href)
let id = currentUrl.searchParams.get('id')
console.log(id);
httpRequest(`http://192.168.1.41:18080/system/info/${id}`, 'GET') 
    .then(res => {
      infoList = res
      // console.log('infoList', infoList);
      // 基本信息
      document.querySelector('.current a').innerHTML = infoList.chexingmingcheng1.substring(0, 5)
      // 车型号
      const carName =  document.querySelector('.subnav-title-name')
      carName.innerHTML = infoList.chexinghao
      // 指导价
      document.querySelector('.zdprice i').innerHTML = infoList.changjiazhidaojia1
      // 级别
      document.querySelector('.models_params li:nth-child(1)').innerHTML = '级别：' + infoList.chrjibie
      // 排量
      document.querySelector('.models_params li:nth-child(2)').innerHTML = '排量：' + infoList.pailiang
      // 能源
      document.querySelector('.models_params li:nth-child(3)').innerHTML = '能源：' + infoList.nengyuan
      // 驱动
      document.querySelector('.models_params li:nth-child(4)').innerHTML = '驱动：' + infoList.qudong
      // 国别
      document.querySelector('.models_params li:nth-child(5)').innerHTML = '国别：' + infoList.guobie
      // 变速箱
      document.querySelector('.models_params li:nth-child(6)').innerHTML = '变速箱：' + infoList.biansuxiang
      // 结构
      document.querySelector('.models_params li:nth-child(7)').innerHTML = '结构：' + infoList.jiegou

      // 车型名称1
      document.querySelector('.tableline tr:nth-child(1) .name_d .name').innerHTML = infoList.chexingmingcheng1
      document.querySelector('.tableline tr:nth-child(1) .price_d').innerHTML = infoList.changjiazhidaojia1
      document.querySelector('.tableline tr:nth-child(1) .type_d').innerHTML = infoList.huanbaobiaozhun
      document.querySelector('.tableline tr:nth-child(1) .kw_d').innerHTML = infoList.zuidagonglv
      document.querySelector('.tableline tr:nth-child(1) .size_d').innerHTML = infoList.changkuangao
      document.querySelector('.tableline tr:nth-child(1) .speed_d').innerHTML = infoList.zuigaochesu
      // 车型名称2
      document.querySelector('.tableline tr:nth-child(2) .name_d .name').innerHTML = infoList.chexingmingcheng2
      document.querySelector('.tableline tr:nth-child(2) .price_d').innerHTML = infoList.changjiazhidaojia2
      document.querySelector('.tableline tr:nth-child(2) .type_d').innerHTML = infoList.huanbaobiaozhun
      document.querySelector('.tableline tr:nth-child(2) .kw_d').innerHTML = infoList.zuidagonglv
      document.querySelector('.tableline tr:nth-child(2) .size_d').innerHTML = infoList.changkuangao
      document.querySelector('.tableline tr:nth-child(2) .speed_d').innerHTML = infoList.zuigaochesu
      // 车型名称3
      document.querySelector('.tableline tr:nth-child(3) .name_d .name').innerHTML = infoList.chexingmingcheng3
      document.querySelector('.tableline tr:nth-child(3) .price_d').innerHTML = infoList.changjiazhidaojia3
      document.querySelector('.tableline tr:nth-child(3) .type_d').innerHTML = infoList.huanbaobiaozhun
      document.querySelector('.tableline tr:nth-child(3) .kw_d').innerHTML = infoList.zuidagonglv
      document.querySelector('.tableline tr:nth-child(3) .size_d').innerHTML = infoList.changkuangao
      document.querySelector('.tableline tr:nth-child(3) .speed_d').innerHTML = infoList.zuigaochesu
      // 面包屑
      document.querySelector('.chejibie').innerHTML = infoList.chrjibie
      document.querySelector('.chexinghao').innerHTML = infoList.qichepinpai + infoList.chexinghao
      document.querySelector('.shouye').addEventListener('click', function() {
        location.href = `../index.html?qichepinpai=${infoList.qichepinpai}`
      })
      document.querySelector('.chejibie').addEventListener('click', function() {
        location.href = `../index.html?qichepinpai=${infoList.qichepinpai}`
      })
      document.querySelector('.chexinghao').addEventListener('click', function() {
        location.href = `../index.html?qichepinpai=${infoList.qichepinpai}`
      })
      
      // 图片
      const titlePic = document.querySelector('.models_pics img')
      titlePic.src = `http://192.168.1.41:10002/resources/${infoList.qichepinpai}${infoList.chexinghao}/01.jpg`
      const ul = document.querySelectorAll('.piclist img')
      ul.forEach((item, index) => {
        item.src = `http://192.168.1.41:10002/resources/${infoList.qichepinpai}${infoList.chexinghao}/0${index+2}.jpg`
      })
    })
    .catch(error => console.error('Error:', error));
